import 'package:aone_common/common.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:prototype/components/v_button.dart';
import 'package:prototype/components/v_dialog.dart';
import 'package:prototype/components/v_dropdown.dart';
import 'package:prototype/components/v_searchable_dropdown.dart';
import 'package:prototype/components/v_text_field.dart';

import '../../../theme/custom_theme.dart';

class RegisterForm extends StatelessWidget {
  const RegisterForm({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final state = RegisterLogic.to.state;
    final logic = RegisterLogic.to;
    final _theme = CustomTheme.colorSchema;
    return SingleChildScrollView(
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.symmetric(
        horizontal: 20.0.h,
        vertical: 20.0.h,
      ),
      child: FormBuilder(
        key: state.formKey,
        child: Column(
          children: [
            ...state.formFields.map(
              (e) {
                if (e.fieldType == FieldType.checkbox) {
                  return FormBuilderCheckbox(
                    name: e.key,
                    initialValue: false,
                    activeColor: _theme.primaryColor,
                    decoration: const InputDecoration(
                      enabledBorder: InputBorder.none,
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      errorBorder: InputBorder.none,
                      disabledBorder: InputBorder.none,
                      contentPadding: EdgeInsets.zero,
                    ),
                    title: RichText(
                      text: TextSpan(
                        children: [
                          TextSpan(
                            text: 'reg.agreement.1'.tr,
                            style: TextStyle(color: _theme.neutral),
                          ),
                          TextSpan(
                            recognizer: TapGestureRecognizer()
                              ..onTap = () {
                                SmartDialog.show(widget: _agreementDialog());
                              },
                            text: 'reg.agreement.2'.tr,
                            style: const TextStyle(color: Colors.blue),
                          ),
                        ],
                      ),
                    ),
                    validator: e.validator,
                  );
                }
                if (e.key == 'bankname') {
                  return VSearchableDropDown(
                    keyName: e.key,
                    labelText: e.label.tr,
                    isRequired: e.required,
                    validators: e.validator,
                    items: state.bankList.map((element) {
                      return element.name;
                    }).toList(),
                  );
                }
                if (e.key == 'reg_wen') {
                  return VDropDown(
                    labelText: e.label.tr,
                    keyName: e.key,
                    validators: e.validator,
                    placeholder: e.remarks.tr,
                    items: state.questionList.map((element) {
                      return DropdownMenuItem(
                        value: element,
                        child: Text(element),
                      );
                    }).toList(),
                  );
                }
                return VTextField(
                  keyName: e.key,
                  labelText: e.label.tr,
                  textInputType: e.inputType,
                  isPwd: e.inputType == TextInputType.visiblePassword,
                  placeholder: e.remarks.tr,
                  validators: e.validator,
                  isRequired: e.required,
                );
              },
            ).toList(),
            VButton.primary(
              'register'.tr,
              onPressed: () => logic.handleSubmit(),
            ),
          ],
        ),
      ),
    );
  }

  Widget _agreementDialog() {
    final state = RegisterLogic.to.state;

    return VBaseDialog(
      titleText: 'reg.agreement.2'.tr,
      content: SingleChildScrollView(
        child: HtmlWidget(
          state.agreement,
          textStyle: TextStyle(
            color: Colors.white,
            fontSize: 15.sp,
          ),
        ),
      ),
    );
  }
}
